package com.joint.web.action.main;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.fz.us.base.bean.Result;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.joint.base.entity.Company;
import com.joint.base.entity.Users;
import com.joint.base.service.AdminService;
import com.joint.base.service.CompanyService;
import com.joint.base.util.DataUtil;
import com.joint.base.util.StringUtils;
import com.joint.core.bean.EstEnum;
import com.joint.core.entity.AdminInfo;
import com.joint.core.service.AdminInfoService;
import com.joint.web.action.BaseAdminAction;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hpj on 2015/5/6.
 */
@ParentPackage("main")
public class AjaxAuthcodeAction extends BaseAdminAction {

    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    private AdminService adminService;
    @Resource
    private CompanyService companyService;


    private static final long serialVersionUID = -5383463207248344967L;

    private String num;
    private String companyId;
    private List<Company> companyList;

    public String genCode(){
        Result r = new Result(400,"请输入充值个数",400,null);

        if(StringUtils.isEmpty(num)) return ajaxHtmlAppResult(r);

        adminInfoService.createAuthCode(Integer.parseInt(num));
        r = new Result(200,"生成"+num+"个充值码",200,null);
        return ajaxHtmlAppResult(r);
    }

    /**
     * 合同签署页面
     * @return
     */
    public String bindCompany(){
        pager = new Pager(0);
        String[] codeArr = keyId.split(",");
        num = String.valueOf(codeArr.length);

        Map<String,Object> rmap = Maps.newHashMap();
        rmap.put("state",BaseEnum.StateEnum.Enable);
        pager = companyService.findByPager(pager,rmap);
        companyList = (List<Company>) (pager.getTotalCount()>0?pager.getList(): Lists.newArrayList());
        return "bindCompany";
    }

    /**
     * 合同签署操作
     * @return
     */
    public String bindCom(){
        Result r = new Result(400,"请选择签署公司",400,null);
        if(StringUtils.isEmpty(companyId)) return ajaxHtmlAppResult(r);
        Company company = companyService.get(companyId);
        List<AdminInfo> adminInfoList = Lists.newArrayList();
        String[] arr = keyId.split(",");
        for(String keyId : arr){
            if(StringUtils.isEmpty(keyId)) continue;
            AdminInfo adminInfo =adminInfoService.get(keyId);
            if(!adminInfo.getStatus().equals(EstEnum.AdminStatusEnum.unuse)){
                r.setMessage("签署的充值码中含有已使用的充值码");
                return ajaxHtmlAppResult(r);
            }
            adminInfoList.add(adminInfo);
        }
        r = adminInfoService.bindCom(company,adminInfoList);
        return ajaxHtmlAppResult(r);
    }

    public String list(){
        pager = new Pager();
        pager.setPageSize(rows);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);

        //Company com = usersService.getCompanyByUser();
        //Users users = usersService.getLoginInfo();

        List<AdminInfo> adminInfoList= (List<AdminInfo>) adminInfoService.findByPagerAndCompany(pager,null,null,new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable}).getList();


        List<JSONObject> dataRows=new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;
        if(adminInfoList!=null && adminInfoList.size()>0){
            for(AdminInfo adminInfo:adminInfoList){
                rMap = new HashMap<String, Object>();
                rMap.put("id",adminInfo.getId());
                rMap.put("authCode",adminInfo.getAuthCode());
                rMap.put("company", StringUtils.isNoneEmpty(adminInfo.getCompanyId())?companyService.get(adminInfo.getCompanyId()).getName():"");
                rMap.put("bindDate", DataUtil.DateToString(adminInfo.getBindDate(), "yyyy-MM-dd"));
                rMap.put("createDate", DataUtil.DateToString(adminInfo.getCreateDate(), "yyyy-MM-dd"));
                rMap.put("creater", adminInfo.getCreater().getName());
                rMap.put("status", adminInfo.getStatus() != null? adminInfo.getStatus().value():"暂未使用");
                rMap.put("days",adminInfo.getDays());
                rMap.put("use","");

                String adminId = adminInfo.getAdminId();
                if(StringUtils.isNotEmpty(adminId)){
                    Users users = usersService.get("adminId",adminId);
                    rMap.put("use",users.getName());
                }
                dataRows.add(JSONObject.fromObject(rMap));
            }
        }

        long total =pager.getTotalCount();
        if(total%rows==0){
            total = total/rows;
        }else{
            if(total<rows){
                total = 1;
            }else{
                total = total/rows + 1;
            }
        }

        data.put("dataRows",dataRows);
        data.put("page",page);
        data.put("rows",rows);
        data.put("total",total);
        data.put("records",pager.getTotalCount());
        return  ajaxJson(JSONObject.fromObject(data).toString());
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public List<Company> getCompanyList() {
        return companyList;
    }

    public void setCompanyList(List<Company> companyList) {
        this.companyList = companyList;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
}
